package com.eyas.business.controllor.coachinfo;

import com.eyas.business.model.jdbcEntity.table.CoachInfo;
import com.eyas.business.model.pojo.coachinfo.QueryParamDTO;
import com.eyas.business.model.pojo.coachinfo.QueryResultDTO;
import com.eyas.business.model.pojo.formselects.FormSelectsResult;
import com.eyas.business.service.coachinfo.CoachInfoService;
import com.eyas.core.model.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/6 15:30
 * @Description:
 */
@Controller
@RequestMapping("/eyas/coachinfo/")
public class CoachInfoAction {
    @Autowired
    private CoachInfoService coachInfoService;

    @GetMapping("/queryCoachInfoForPage")
    @ResponseBody
    public PageResult<QueryResultDTO> queryCoachInfoForPage(QueryParamDTO dto,
                                                            @RequestParam(defaultValue = "1") int page,
                                                            @RequestParam(defaultValue = "10") int limit) {
        PageResult<QueryResultDTO> pageResult = coachInfoService.queryCoachInfoForPage(dto,page-1,limit);
        return pageResult;
    }

    @PostMapping("/changeValidflag")
    @ResponseBody
    public String changeValidflag(int coachid) {
        coachInfoService.changeValidflag(coachid);
        return "{\"success\":true}";
    }

    @GetMapping("/edit")
    public String edit(Integer coachid, Model model) {
        if(coachid!=null&&coachid!=0) {
            QueryResultDTO coachInfo = this.coachInfoService.getCoachInfoById(coachid);
            if(coachInfo.getStarttime()!=null)
                coachInfo.setStarttime_str(new SimpleDateFormat("yyyy-MM-dd").format(coachInfo.getStarttime()));
            if(coachInfo.getEndtime()!=null)
                coachInfo.setEndtime_str(new SimpleDateFormat("yyyy-MM-dd").format(coachInfo.getEndtime()));
            model.addAttribute("coachInfo",coachInfo);
        }
        return "/coachinfo/edit";
    }

    @PostMapping("/saveCoachInfo")
    @ResponseBody
    public String saveCoachInfo(QueryParamDTO dto, HttpSession session) {
        coachInfoService.saveCoachInfo(dto,session);
        return "{\"success\":true}";
    }


    @GetMapping("/queryCoachInfoList")
    @ResponseBody
    public List<CoachInfo> queryCoachInfoList() {
        return  coachInfoService.queryCoachInfoList();
    }

    @GetMapping("/queryAssistantList/{recordid}")
    @ResponseBody
    public FormSelectsResult queryAssistantList(@PathVariable(name = "recordid",required = false) Integer recordid) {
        FormSelectsResult result = this.coachInfoService.queryAssistantList(recordid);
        return result;
    }
}
